<?php

namespace App\Library\Logic;

use App\Library\Requests\ProductAlbum\ProductAlbumPageRequest;
use App\Models\ShopProductAlbumModel;

class ShopProductAlbumLogic extends ShopProductAlbumModel
{

    public function paging(ProductAlbumPageRequest $request): array
    {
        $builder = self::query();
        if (USERID === MAINID || POWER === 1) $builder->where('user_main_id', MAINID);
        else $builder->where('publisher_id', USERID);

        if (!is_null($request->keyword)) {
            $builder->where(function ($query) use ($request) {
                $query->orWhere('title', 'like', '%' . $request->keyword . '%')
                    ->orWhere('describe', 'like', '%' . $request->keyword . '%')
                    ->orWhereJsonContains('tag_ids', $request->keyword);
            });
        }

        return paginate($builder->orderByDesc('created_at')->paginate($request->limit));
    }
}
